﻿using Dapper;
using Domain.Entity;
using Domain.Entity.sys;
using Domain.IRepository;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repository
{
    public class InfoRepository : IInfoRepository
    {
        //连接字符串
        string con = "Data Source=121.37.93.71;Initial Catalog=CSSL_ZCTWMS;User ID=sa;Password=geili@2025;Encrypt=False";

        //删除
        public async Task<int> DeleteInfo(int id)
        {
            using (SqlConnection sqlConnection = new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $@"delete from [dbo].[sys_info] where info_id={id}";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }

        //查询
        public async Task<FenYe<Info>> GetInfo(FenYe<Info> fenYe, string? text, string? type, int? status)
        {
            using (SqlConnection sqlConnection = new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();

                //构建WHERE条件
                string whereClause = " WHERE 1=1 ";
                var parameters = new DynamicParameters();

                if (!string.IsNullOrEmpty(text))
                {
                    whereClause += " AND info_text LIKE @text";
                    parameters.Add("@text", $"%{text}%");
                }
                if (!string.IsNullOrEmpty(type))
                {
                    whereClause += " AND info_type LIKE @type";
                    parameters.Add("@type", $"%{type}%");
                }
                if (status != 0 && status != null)
                {
                    whereClause += " AND info_status = @status";
                    parameters.Add("@status", status);
                }

                // 查询总数
                string countSql = $"SELECT COUNT(*) FROM sys_info {whereClause}";
                int totalRows = await sqlConnection.QuerySingleAsync<int>(countSql, parameters);

                // 分页查询，按时间降序
                int offset = (fenYe.CurrentPage - 1) * 10;
                string dataSql = $@"
                                    SELECT * FROM sys_info 
                                    {whereClause}
                                    ORDER BY info_time DESC
                                    OFFSET @offset ROWS 
                                    FETCH NEXT @pageSize ROWS ONLY";

                parameters.Add("@offset", offset);
                parameters.Add("@pageSize", 10);

                List<Info> list = (await sqlConnection.QueryAsync<Info>(dataSql, parameters)).ToList();

                fenYe.List = list;
                fenYe.Rows = totalRows;
                return fenYe;
            }
        }


        //修改信息状态
        public async Task<int> UpdateStatus(int id, int status)
        {
            using (SqlConnection sqlConnection = new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $@"update [dbo].[sys_info] set info_status={status} where info_id={id}";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }
    }
}
